package com.todod.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.todod.entity.ReqRoleFile;
import com.todod.entity.ResultData;
import com.todod.model.PublicCatalog;
import com.todod.model.PublicFile;
import com.todod.model.PublicFileAuthority;
import com.todod.model.UserRole;
import com.todod.service.IUserRoleService;
import com.todod.service.PublicCatalogService;
import com.todod.service.PublicFileAuthorityService;
import com.todod.service.PublicFileService;

@RestController
@RequestMapping("/fileAuthority")
public class PublicFileAuthorityController {

	@Autowired
	public PublicCatalogService publicCatalogService;
	@Autowired
	private PublicFileService publicFileService;
	@Autowired
	private IUserRoleService iUserRoleService;
	@Autowired
	private PublicFileAuthorityService fileAuthorityService;

	/**
	 * 
	 * @param companyId
	 * @param _userId
	 * @param request
	 * @param queryProjectFile
	 * @return
	 */
	@RequestMapping("/getAuthTreeList")
	public ResultData getAuthTreeList(@RequestAttribute Long _userId, Long roleId) {
		// 获取所有目录数据
		List<PublicCatalog> list = publicCatalogService.getListByFlag("M");

		List<PublicCatalog> newList = new ArrayList<PublicCatalog>();
		// 获取所有目录下文件数据
		for (PublicCatalog publicCatalog : list) {
			List<PublicFile> list1 = publicFileService.getFileByGftId(publicCatalog.getId());
			if (list1 != null && list1.size() > 0) {
				for (PublicFile publicFile : list1) {
					// 获取公共数据
					PublicCatalog publicCatalog2 = new PublicCatalog();
					publicCatalog2.setId(publicFile.getId());
					publicCatalog2.setParentId(publicCatalog.getId());
					publicCatalog2.setSortNum(0);
					publicCatalog2.setTypeName(publicFile.getOriginalName());
					publicCatalog2.setFlag("M");
					publicCatalog2.setState("1");
					publicCatalog2.setDepartmentId(null);

					newList.add(publicCatalog2);
				}
			}
		}
		// 将文件添加到文件夹中
		list.addAll(newList);

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("list", list);

		List<UserRole> userRoleList = iUserRoleService.getByRoleId(roleId);
		if (userRoleList != null && userRoleList.size() > 0) {
			List<Long> hashUser = userRoleList.stream().map(x -> x.getUserId()).collect(Collectors.toList());

			Long userId = fileAuthorityService.getUserIdInMax(hashUser);
			List<PublicFileAuthority> list1 = fileAuthorityService.getFileAuthorityByUserId(userId);
			List<Long> hashAuthId = list1.stream().map(x -> x.getFileId()).collect(Collectors.toList());
			map.put("hashAuthId", hashAuthId);
		}

		return ResultData.success("ok", map);
	}

	/**
	 * 
	 * @param _userId
	 * @param reqRoleFile
	 * @return
	 */
	@PostMapping("/saveAuthorit")
	public ResultData saveAuthorit(@RequestAttribute Long _userId, @RequestBody ReqRoleFile reqRoleFile) {
		fileAuthorityService.saveData(_userId, reqRoleFile.getFileId(), reqRoleFile.getUserIds(),
				reqRoleFile.getType());
		return ResultData.success("ok");
	}

	/**
	 * @title saveFileWDAuthorit
	 * @description 角色文档权限分配
	 * @param _userId
	 * @param reqRoleFile
	 * @return
	 */
	@PostMapping("/saveFileWDAuthorit")
	public ResultData saveFileWDAuthorit(@RequestAttribute Long _userId, @RequestBody ReqRoleFile reqRoleFile) {
		fileAuthorityService.saveFileWDAuthorit(_userId, reqRoleFile.getRoleId(), reqRoleFile.getRoleFiles());
		return ResultData.success("ok");
	}

}
